package com.tiantian.quanminxiu.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.jakewharton.rxbinding.view.RxView;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.fragment.Fragment_Main_Attention;
import com.tiantian.quanminxiu.fragment.Fragment_Main_Hall;
import com.tiantian.quanminxiu.fragment.Fragment_Main_Mine;
import com.tiantian.quanminxiu.fragment.Fragment_Main_Ranking;
import com.tiantian.quanminxiu.module.Module_Footer;
import com.tiantian.quanminxiu.utils.APPUtils;
import com.tiantian.quanminxiu.utils.ActivityBase;
import com.tiantian.quanminxiu.utils.CommonEnum;
import com.tiantian.quanminxiu.utils.UserData;
import com.tiantian.quanminxiu.view.MainView;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

public class MainActivity extends ActivityBase implements MainView {

    private List<Fragment> listFragment;
    private Module_Footer mFooter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Init();

        InitView();

    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void InitView() {
        mFooter = (Module_Footer) findViewById(R.id.mFooter);

        int intToPage = getIntent().getIntExtra("ToPage", CommonEnum.MainPageIndex.Hall);

        RxView.clicks(mFooter.vFooter_Hall).map(aVoid -> "hall").subscribe(PageSwitch_Clicks);
        RxView.clicks(mFooter.vFooter_Attention).map(aVoid -> "attention").subscribe(PageSwitch_Clicks);
        RxView.clicks(mFooter.vFooter_Live).map(aVoid -> "live").subscribe(PageSwitch_Clicks);
        RxView.clicks(mFooter.vFooter_Ranking).map(aVoid -> "ranking").subscribe(PageSwitch_Clicks);
        RxView.clicks(mFooter.vFooter_Mine).map(aVoid -> "mine").subscribe(PageSwitch_Clicks);

        listFragment = new ArrayList<>();
        listFragment.add(new Fragment_Main_Hall());
        listFragment.add(new Fragment_Main_Attention());
        listFragment.add(new Fragment_Main_Ranking());
        listFragment.add(new Fragment_Main_Mine());

        PageSwitch(intToPage);
    }

    //region 点击频道切换 PageSwitch_Clicks
    private Action1<String> PageSwitch_Clicks = strValue -> {
        switch (strValue.toLowerCase()) {
            case "hall":
                PageSwitch(CommonEnum.MainPageIndex.Hall);
                break;
            case "attention":
                PageSwitch(CommonEnum.MainPageIndex.Attention);
                break;
            case "live":
                //跳转到二维码扫描
                APPUtils.PhoneLive_To(thisActivity, UserData.Base.User_ID);
                break;
            case "ranking":
                PageSwitch(CommonEnum.MainPageIndex.Ranking);
                break;
            case "mine":
                PageSwitch(CommonEnum.MainPageIndex.Mine);
                break;
        }
    };
    //endregion

    @Override
    public void PageInit(){

    };

    //region PageSwitch(final int PageIndex)
    public void PageSwitch(final int PageIndex) {
        if (PageIndex < 0 || PageIndex >= listFragment.size()) {
            return;
        }
        mFooter.Channel(PageIndex);
        try {
            FragmentTransaction mFragmentTransaction = getSupportFragmentManager().beginTransaction();
            Fragment ThisFragment = listFragment.get(PageIndex);

            if (!ThisFragment.isAdded()) {
                mFragmentTransaction.add(R.id.flMain, ThisFragment);
            }
            if (UserData.Current.Main_PageIndex >= 0 && UserData.Current.Main_PageIndex < listFragment.size()
                    && PageIndex != UserData.Current.Main_PageIndex) {
                if (listFragment.get(UserData.Current.Main_PageIndex).isAdded()) {
                    mFragmentTransaction.hide(listFragment.get(UserData.Current.Main_PageIndex));
                }
            }
            UserData.Current.Main_PageIndex = PageIndex;
            mFragmentTransaction.show(ThisFragment);
            mFragmentTransaction.commitAllowingStateLoss();
        } catch (Exception e) {
            PageSwitch(CommonEnum.MainPageIndex.Hall);
        }
    }
    //endregion

    @Override
    public void getData(JSONArray jsonArray,int iDisplayStart) {

    }

    //region 按两次回退键退出 onKeyDown(int keyCode, KeyEvent event)
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (UserData.Current.Main_PageIndex != CommonEnum.MainPageIndex.Hall) {
                PageSwitch(CommonEnum.MainPageIndex.Hall);
                return true;
            }
            APPUtils.APPExit(thisActivity);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
    //endregion

    @Override
    protected void onResume() {
        super.onResume();
        PageInit();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
